import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',

  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  }
  ,
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/user/index.vue'),
    redirect: "/shouye",
    children: [
      {
        path: '/shouye',
        name: 'shouye',
        component: () => import('../views/user/user.vue')
      }, {
        path: '/shangpin',
        name: 'shangpin',
        component: () => import('../views/user/shangpin.vue'),
        meta: { title: "商品管理", path: "/shangpin" }
      }, {
        path: '/xiangche',
        name: 'xiangche',
        component: () => import('../views/user/xiangche.vue')
        , meta: { title: "相册管理", path: "/xiangche" }
      },
    ]
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/order/index.vue')
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/shop/index.vue')
  },
  {
    path: '/VIP',
    name: 'VIP',
    component: () => import('../views/VIP/index.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token")) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next("/login");
    }
  }
});

//全局的路由守卫 守卫所有的跳转，如果已经登陆了就正常执行跳转
// 如果没有登陆就跳转到登陆页面 会有一个死循环 所以要判断是否是登陆页面 如果是登陆页面就继续

export default router
